package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.HiolabsRegion;
import com.ruoyi.fun.service.IHiolabsRegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 地区Controller
 * 
 * @author ruoyi
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/fun/region")
public class HiolabsRegionController extends BaseController
{
    @Autowired
    private IHiolabsRegionService hiolabsRegionService;

    /**
     * 查询地区列表
     */
    @PreAuthorize("@ss.hasPermi('fun:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(HiolabsRegion hiolabsRegion)
    {
        startPage();
        List<HiolabsRegion> list = hiolabsRegionService.selectHiolabsRegionList(hiolabsRegion);
        return getDataTable(list);
    }

    /**
     * 导出地区列表
     */
    @PreAuthorize("@ss.hasPermi('fun:region:export')")
    @Log(title = "地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HiolabsRegion hiolabsRegion)
    {
        List<HiolabsRegion> list = hiolabsRegionService.selectHiolabsRegionList(hiolabsRegion);
        ExcelUtil<HiolabsRegion> util = new ExcelUtil<HiolabsRegion>(HiolabsRegion.class);
        util.exportExcel(response, list, "地区数据");
    }

    /**
     * 获取地区详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hiolabsRegionService.selectHiolabsRegionById(id));
    }

    /**
     * 新增地区
     */
    @PreAuthorize("@ss.hasPermi('fun:region:add')")
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HiolabsRegion hiolabsRegion)
    {
        return toAjax(hiolabsRegionService.insertHiolabsRegion(hiolabsRegion));
    }

    /**
     * 修改地区
     */
    @PreAuthorize("@ss.hasPermi('fun:region:edit')")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HiolabsRegion hiolabsRegion)
    {
        return toAjax(hiolabsRegionService.updateHiolabsRegion(hiolabsRegion));
    }

    /**
     * 删除地区
     */
    @PreAuthorize("@ss.hasPermi('fun:region:remove')")
    @Log(title = "地区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hiolabsRegionService.deleteHiolabsRegionByIds(ids));
    }

    // ========== 以下为APP端公开接口 ==========

    /**
     * 获取省份列表（APP端公开接口）
     */
    @Anonymous
    @GetMapping("/provinces")
    public AjaxResult getProvinces()
    {
        List<HiolabsRegion> provinces = hiolabsRegionService.selectHiolabsRegionListByType(1);
        return success(provinces);
    }

    /**
     * 根据父级ID获取子级地区列表（APP端公开接口）
     * 用于级联查询：传入省份ID获取城市列表，传入城市ID获取区县列表
     */
    @Anonymous
    @GetMapping("/children/{parentId}")
    public AjaxResult getChildrenByParentId(@PathVariable("parentId") Long parentId)
    {
        List<HiolabsRegion> children = hiolabsRegionService.selectHiolabsRegionListByParentId(parentId);
        return success(children);
    }

    // ========== 以下为兼容性接口，保留原有功能 ==========

    /**
     * 根据省份ID获取城市列表（APP端公开接口）
     */
    @Anonymous
    @GetMapping("/cities/{provinceId}")
    public AjaxResult getCitiesByProvinceId(@PathVariable("provinceId") Long provinceId)
    {
        List<HiolabsRegion> cities = hiolabsRegionService.selectHiolabsRegionListByTypeAndParentId(2, provinceId);
        return success(cities);
    }

    /**
     * 根据城市ID获取区县列表（APP端公开接口）
     */
    @Anonymous
    @GetMapping("/districts/{cityId}")
    public AjaxResult getDistrictsByCityId(@PathVariable("cityId") Long cityId)
    {
        List<HiolabsRegion> districts = hiolabsRegionService.selectHiolabsRegionListByTypeAndParentId(3, cityId);
        return success(districts);
    }

    /**
     * 根据类型获取地区列表（APP端公开接口）
     * @param type 地区类型(1:省份 2:城市 3:区县)
     */
    @Anonymous
    @GetMapping("/list-by-type/{type}")
    public AjaxResult getRegionListByType(@PathVariable("type") Integer type)
    {
        List<HiolabsRegion> regions = hiolabsRegionService.selectHiolabsRegionListByType(type);
        return success(regions);
    }
}